package com.frank.loadinglibrary.common.ring;

import com.frank.loadinglibrary.utils.Indicator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import java.util.ArrayList;

public class CircularRingComponent extends BaseRingLoadingComponent implements Component.DrawTask {

    private Paint mPaint;

    private Paint mPaintPro;

    private float mWidth = 0f;

    private float mPadding = 5.0f;

    private float startAngle = 0f;

    RectFloat rectF = new RectFloat();

    //动画执行指示器
    protected Indicator indicator;

    private float mRingWidth = 8;

    private float sweepAngle = 100;

    public CircularRingComponent(Context context) {
        this(context, null);
    }

    public CircularRingComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public CircularRingComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init();
    }

    public CircularRingComponent(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        init();
    }

    public CircularRingComponent setRingWidth(float mRingWidth) {
        this.mRingWidth = mRingWidth;
        return this;
    }

    public CircularRingComponent setSweepAngle(float sweepAngle) {
        this.sweepAngle = sweepAngle;
        return this;
    }

    @Override
    protected void stopAnimators() {
        indicator.stop();
    }

    @Override
    protected void startAnimators() {
        indicator.start();
    }

    @Override
    protected void releaseSource() {

    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        int width = getWidth() <= getHeight() ? getWidth() : getHeight();
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, width / 2 - mPadding, mPaintPro);
        rectF = new RectFloat(getWidth() / 2 - width / 2 + mPadding, getHeight() / 2 - width / 2 + mPadding, getWidth() / 2 + width / 2 - mPadding, getHeight() / 2 + width / 2 - mPadding);
        canvas.drawArc(rectF, new Arc(startAngle, sweepAngle, false), mPaint);//第四个参数是否显示半径
    }

    private void init() {
        addDrawTask(this);
        initPaint();
        setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                if (autoStart) {
                    startAnimators();
                }
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
        indicator = new Indicator<AnimatorValue>() {
            @Override
            public ArrayList<AnimatorValue> onCreateAnimators() {
                return CircularRingComponent.this.onCreateAnimators();
            }
        };
    }

    private void initPaint() {

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(mRingWidth);

        mPaintPro = new Paint();
        mPaintPro.setAntiAlias(true);
        mPaintPro.setStyle(Paint.Style.STROKE_STYLE);
        mPaintPro.setColor(new Color(Color.argb(100, 255, 255, 255)));
        mPaintPro.setStrokeWidth(mRingWidth);
    }

    private ArrayList<AnimatorValue> onCreateAnimators() {
        ArrayList<AnimatorValue> animators = new ArrayList<>();
        AnimatorValue rorateAnim = new AnimatorValue();
        rorateAnim.setDuration(durtion);
        rorateAnim.setLoopedCount(-1);
        indicator.addUpdateListener(rorateAnim, (animatorValue, v) -> {
            //设定 0.3 - 1之间缩放
            startAngle = 360 * v;
            invalidate();
        });
        animators.add(rorateAnim);
        return animators;
    }

}
